<template>
  <el-dialog title="商品查询" :visible.sync="visible" fullscreen append-to-body center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-divider content-position="left">查询条件</el-divider>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="searchHandle">
      <el-form-item label="商品代码">
          <el-input v-model="dataForm.code"></el-input>
        </el-form-item>
        <el-form-item label="商品条码">
          <el-input v-model="dataForm.barcode"></el-input>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model="dataForm.name"></el-input>
        </el-form-item>
        <el-form-item label="商品类别">
          <el-select v-model="dataForm.ctgId" disabled>
            <el-option v-for="item in ctgOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="制造商">
          <el-input v-model="dataForm.supplyName">
            <el-button slot="append" @click="openDialog('supply')">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="商品品种">
          <el-input v-model="dataForm.brandName">
            <el-button slot="append" @click="openDialog('brand')">选择</el-button>
          </el-input>
        </el-form-item>
        <!-- <el-form-item label="仓位">
          <el-select v-model="dataForm.stockId">
            <el-option v-for="item in stockOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="厂家色号" v-if="[1,6,8].includes(dataForm.ctgId)">
          <el-input v-model="dataForm.manufacturerColorNumber"></el-input>
        </el-form-item>
        <el-form-item label="镜架尺寸" v-if="[1,6,8].includes(dataForm.ctgId)">
          <el-input v-model="dataForm.goodsSize"></el-input>
        </el-form-item>
        <el-form-item label="镜架材质" v-if="dataForm.ctgId == 1">
          <el-select v-model="dataForm.frameMaterial" clearable>
            <el-option v-for="item in frameMaterialData" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工艺类型" v-if="dataForm.ctgId == 1">
          <el-select v-model="dataForm.craftType" clearable>
            <el-option v-for="item in craftTypeData" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="配件类型" v-if="dataForm.ctgId == 2">
          <el-select v-model="dataForm.accessoriesType" clearable>
            <el-option v-for="item in accessoryTypeData" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="球镜范围" v-if="[3,4].includes(dataForm.ctgId)">
          <el-row>
            <el-col :span="11">
              <el-input v-model="dataForm.mirrorBallMin"></el-input>
            </el-col>
            <el-col :span="2" style="text-align: center;"> - </el-col>
            <el-col :span="11">
              <el-input v-model="dataForm.mirrorBallMax"></el-input>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="柱镜范围" v-if="[3,4].includes(dataForm.ctgId)">
          <el-row>
            <el-col :span="11">
              <el-input v-model="dataForm.columnMirrorMin"></el-input>
            </el-col>
            <el-col :span="2" style="text-align: center;"> - </el-col>
            <el-col :span="11">
              <el-input v-model="dataForm.columnMirrorMax"></el-input>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="下加光" v-if="dataForm.ctgId == 3">
          <el-row>
            <el-col :span="11">
              <el-input v-model="dataForm.toTheLightMin"></el-input>
            </el-col>
            <el-col :span="2" style="text-align: center;"> - </el-col>
            <el-col :span="11">
              <el-input v-model="dataForm.toTheLightMax"></el-input>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="定做类型" v-if="[3,4].includes(dataForm.ctgId)">
          <el-select v-model="dataForm.customizeType" clearable>
            <el-option v-for="item in bizData.customizeTypeData" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="材料分类" v-if="dataForm.ctgId == 3">
          <el-select v-model="dataForm.lensMaterial" clearable>
            <el-option v-for="item in bizData.lensMaterial" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="镜片分类" v-if="dataForm.ctgId == 3">
          <el-select v-model="dataForm.photometricClassification" clearable>
            <el-option v-for="item in bizData.photometricClassification" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="镜片功能" v-if="dataForm.ctgId == 3">
          <el-select v-model="dataForm.lensFunction" clearable>
            <el-option v-for="item in lensFunction" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="渐进片分类" v-if="dataForm.ctgId == 3">
          <el-select v-model="dataForm.progressiveSliceClassification" clearable>
            <el-option v-for="item in bizData.progressiveSliceClassification" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="曲率" v-if="dataForm.ctgId == 4">
          <el-input v-model="dataForm.curvature"></el-input>
        </el-form-item>
        <el-form-item label="直径" v-if="dataForm.ctgId == 4">
          <el-input v-model="dataForm.diameter"></el-input>
        </el-form-item>
        <el-form-item label="使用类型" v-if="dataForm.ctgId == 4">
          <el-select v-model="dataForm.useType" clearable>
            <el-option v-for="item in bizData.useType" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="抛弃型类型" v-if="dataForm.ctgId == 4">
          <el-select v-model="dataForm.disusedClassification" clearable>
            <el-option v-for="item in bizData.disusedClassification" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="主容量" v-if="dataForm.ctgId == 5">
          <el-input v-model="dataForm.primaryCapacity"></el-input>
        </el-form-item>
        <el-form-item label="次容量" v-if="dataForm.ctgId == 5">
          <el-input v-model="dataForm.secondaryCapacity"></el-input>
        </el-form-item>
        <el-form-item label="型号">
          <el-input v-model="dataForm.spec"></el-input>
        </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchHandle">查询</el-button>
        <el-button type="info" @click="clearSearchForm">清空</el-button>
      </el-form-item>
    </el-form>
    <el-divider content-position="left">查询数据</el-divider>
    <el-row>
      <el-button type="primary" style="float: right;margin-bottom: 3px;" @click="checkAndClose">选择并关闭</el-button>
    </el-row>
    <div class="avue-crud">
      <el-table :data="dataList" height="calc(100vh - 215px)" ref="goodsTable" border v-loading="dataListLoading" @select="onTableSelect" @selection-change="handleSelectionChange">
      <el-table-column key="1" type="selection" width="50"></el-table-column>
      <el-table-column key="2" prop="code" label="商品代码" width="130"></el-table-column>
      <el-table-column key="3" prop="viewGoodsName" label="商品名称"></el-table-column>
      <el-table-column key="4" prop="spec" label="型号" v-if="![3,4].includes(dataForm.ctgId)"></el-table-column>
      <el-table-column key="5" label="单位">
        <template slot-scope="scope">
          {{getValueByKey(unitTypeData,scope.row.unitType)}}
        </template>
      </el-table-column>
      <el-table-column key="7" prop="price" label="销售价格"></el-table-column>
      <el-table-column key="8" prop="mirrorBall" label="老花镜度数" v-if="dataForm.ctgId == 8"></el-table-column>
      <el-table-column key="9" prop="manufacturerColorNumber" label="厂家色号" v-if="[1,6,8].includes(dataForm.ctgId)"></el-table-column>
      <el-table-column key="10" prop="frameMaterial" label="镜架材质" v-if="[1].includes(dataForm.ctgId)">
        <template slot-scope="scope">
          {{getValueByKey(frameMaterialData,scope.row.frameMaterial)}}
        </template>
      </el-table-column>
      <el-table-column key="11" prop="goodsSize" label="镜架尺寸" v-if="[1,6,8].includes(dataForm.ctgId)"></el-table-column>
      <el-table-column key="12" label="配件型" v-if="dataForm.ctgId == 2">
        <template slot-scope="scope">
          {{getValueByKey(accessoryTypeData,scope.row.accessoriesType)}}
        </template>
      </el-table-column>
      <!-- 镜片 -->
      <el-table-column key="13" prop="mirrorBall" label="球镜" v-if="[3,4].includes(dataForm.ctgId)"></el-table-column>
      <el-table-column key="14" prop="columnMirror" label="柱镜" v-if="[3,4].includes(dataForm.ctgId)"></el-table-column>
      <el-table-column key="15" prop="toTheLight" label="下加光" v-if="dataForm.ctgId == 3"></el-table-column>
      <el-table-column key="16" prop="refractiveIndex" label="折射率" v-if="dataForm.ctgId == 3"></el-table-column>
      <el-table-column key="17" label="镜片分类" v-if="dataForm.ctgId == 3">
        <template slot-scope="scope">
          {{getValueByKey(bizData.photometricClassification,scope.row.photometricClassification)}}
        </template>
      </el-table-column>
      <el-table-column key="18" label="材料分类" v-if="dataForm.ctgId == 3">
        <template slot-scope="scope">
          {{getValueByKey(bizData.lensMaterial,scope.row.lensMaterial)}}
        </template>
      </el-table-column>
      <el-table-column key="19" label="渐进片分类" v-if="dataForm.ctgId == 3">
        <template slot-scope="scope">
          {{getValueByKey(bizData.progressiveSliceClassification,scope.row.progressiveSliceClassification)}}
        </template>
      </el-table-column>
      <el-table-column key="20" label="镜片功能" v-if="dataForm.ctgId == 3">
        <template slot-scope="scope">
          {{getValueByKey(lensFunction,scope.row.lensFunction)}}
        </template>
      </el-table-column>
      <el-table-column key="21" prop="curvature" label="曲率" v-if="dataForm.ctgId == 4"></el-table-column>
      <el-table-column key="22" prop="diameter" label="直径" v-if="dataForm.ctgId == 4"></el-table-column>
      <el-table-column key="23" label="使用类型" v-if="dataForm.ctgId == 4">
        <template slot-scope="scope">
          {{getValueByKey(bizData.useType,scope.row.useType)}}
        </template>
      </el-table-column>
      <el-table-column key="24" label="抛弃类型" v-if="dataForm.ctgId == 4">
        <template slot-scope="scope">
          {{getValueByKey(bizData.disusedClassification,scope.row.disusedClassification)}}
        </template>
      </el-table-column>
      <el-table-column key="25" prop="primaryCapacity" label="主容量" v-if="dataForm.ctgId == 5"></el-table-column>
      <el-table-column key="26" prop="secondaryCapacity" label="次容量" v-if="dataForm.ctgId == 5"></el-table-column>
      <el-table-column key="27" prop="goodsQuantity" label="库存数量" v-if="type == '2'"></el-table-column>

      <!-- <el-table-column key="24" prop="goodsQuantity" header-align="center" align="center" label="库存数量"></el-table-column> -->
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="avue-crud__pagination">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    </div>
        <search-supply v-if="supplyVisible" ref="supplyDialog" @refreshData="getSupplyId"></search-supply>
    <search-brand v-if="brandVisible" ref="brandDialog" @refreshData="getBrandId"></search-brand>
  </el-dialog>
</template>

<script>
  import {filterParams, deduplicationBykey} from '@/util/tools'
  import { remote } from '@/api/admin/dict'
  import { getCtgDataList } from "@/api/eims/check";
  import {  getCheckGoodsInfo, getCheckGoodsByFull, returnGoodsList } from "@/api/eims/goods";

  import { getGlassStorageSaleByPage } from '@/api/eims/storage'
  import { mapGetters } from "vuex";
  import SearchSupply from '@/components/business/SearchSupply'
  import SearchBrand from '@/components/business/SearchBrand'

  export default {
    data() {
      return {
        visible: false,
        type: '',
        dataForm: {},
        dataList: [],
        ctgOptions: [],
        unitTypeData: [],
        frameMaterialData: [],
        lensFunction: [],
        craftTypeData: [],
        accessoryTypeData: [],
        selected: true,
        rowItem: {},
        dataListLoading: false,
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        supplyVisible: false,
        brandVisible: false
      };
    },
    components:{
      SearchSupply,
      SearchBrand
    },
    computed: {
      ...mapGetters(['bizData'])
    },
    methods: {
      init(data) {
        this.visible = true
        remote('unit_type').then(result => {
          this.unitTypeData = result.data.data
        })
        remote('frame_material').then(result => {
          this.frameMaterialData = result.data.data
        })
        remote('craft_type').then(result => {
          this.craftTypeData = result.data.data
        })
        remote('lens_function').then(result => {
          this.lensFunction = result.data.data
        })
        remote('accessory_type').then(result => {
          this.accessoryTypeData = result.data.data
        })
        this.dataForm = data.paramsData;
        this.type = data.paramsData.checkType
        this.tableData = data.tableData;
        this.selectList = data.tableData
        this.dataList = []
        if(this.dataForm.ctgId == '3' || this.dataForm.ctgId == '4'){
          this.$set(this.dataForm,'customizeType', '0')
        }else{
          this.$set(this.dataForm,'customizeType', '')
        }

        this.getCtgList();
      },
			//	查询数据
			searchHandle() {
				this.pageIndex = 1
				this.getDataList()
			},
			//	清空查询表单
      clearSearchForm() {
        for (let key in this.dataForm) {
          if(key =="ctgId" || key == "stockId"){

          }else{
            this.dataForm[key] = "";
          }

        }
      },
      getDataList() {
        let params1 = {
          current: this.pageIndex,
          size: this.pageSize
        }

        let params2 = filterParams(this.dataForm)
        this.dataListLoading = true
        if(this.type == '1'){
          getCheckGoodsByFull(Object.assign(params1, params2)).then(result => {
            this.dataListLoading = false
            this.dataList = result.data.data.records
            this.totalPage = result.data.data.total
            this.$nextTick(() => {
              this.selectList.forEach(item => {
                for (let i = 0; i < this.dataList.length; i++) {
                  if (item.id == this.dataList[i].id && item.batch == this.dataList[i].batch && item.invalidStr == this.dataList[i].invalidStr) {
                    this.$refs.goodsTable.toggleRowSelection(this.dataList[i], true);
                  }
                }
              });
            })
          })
        }else if(this.type == '2'){
          returnGoodsList(Object.assign(params1, params2)).then(result => {
            this.dataListLoading = false
            this.dataList = result.data.data.records
            this.totalPage = result.data.data.total
            this.$nextTick(() => {
              this.selectList.forEach(item => {
                for (let i = 0; i < this.dataList.length; i++) {
                  if (item.id == this.dataList[i].id && item.batch == this.dataList[i].batch && item.invalidStr == this.dataList[i].invalidStr) {
                    this.$refs.goodsTable.toggleRowSelection(this.dataList[i], true);
                  }
                }
              });
            })
          })
        }
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val;
        this.pageIndex = 1;
        this.getDataList();
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val;
        this.getDataList();
      },
      //  判断当前操作的属性是勾选还是取消勾选
      onTableSelect(rows,row) {
        this.selected = rows.length && rows.indexOf(row) !== -1
        this.rowItem = row
      },
      handleSelectionChange(val) {
        if(this.selected) {
          this.selectList = deduplicationBykey(this.selectList.concat(val),'id','batch','invalidStr')
        }else{
          this.selectList.forEach((item,index) => {
            if(item.id == this.rowItem.id){
              this.selectList.splice(index,1)
            }
          })
        }
      },
      checkAndClose() {
        this.$emit('refreshForm', this.selectList)
        this.visible = false
        this.dataList = []
        this.totalPage = 0
      },
      getCtgList() {
        // getCtgDataList().then(result => {
        //   let list = result.data.data.records;
        //   let options = [];
        //   list.forEach(item => {
        //     options.push({
        //       value: item.id,
        //       name: item.name
        //     });
        //   });
        //   this.ctgOptions = options;
        // });
        getCtgDataList().then(result => {
          this.ctgOptions = result.data.data.records
        })
      },
      getValueByKey(data,key) {
        let obj = {}
        obj = data.find(item => {
          return item.value == key
        })
        if(obj && obj.label){
          return obj.label
        }
      },
       openDialog(type) {
        if(type == 'supply'){
          if(this.dataForm.ctgId){
            let data = { ctgId: this.dataForm.ctgId }
            this.supplyVisible = true
            this.$nextTick(() => {
              this.$refs.supplyDialog.init(data)
            })
          }else{
            this.$message.warning('请选择采购类型')
          }
        }else if(type == 'brand'){
          if(this.dataForm.supplyId){
            let data = {ctgId:this.dataForm.ctgId,supplyId:this.dataForm.supplyId}
            this.brandVisible = true
            this.$nextTick(() => {
              this.$refs.brandDialog.init(data)
            })
          }else{
            this.$message.warning('请选择制造商')
          }

        }
      },
      getSupplyId(data) {
        this.$set(this.dataForm,'supplyName',data.name)
        this.dataForm.supplyId = data.id
      },
      getBrandId(data) {
        this.dataForm.brandId = data.id
        this.$set(this.dataForm,'brandName',data.name)
      },
      getValueByKey(data,key) {
        let obj = {}
        obj = data.find(item => {
          return item.value == key
        })
        if(obj && obj.label){
          return obj.label
        }
      },
      formatter(row, column, cellValue, index) {
        switch (cellValue) {
          case 1:
            return '合金'
            break
          case 2:
            return '钛合金'
            break
          case 3:
            return 'K金'
            break
          case 4:
            return '板材'
            break
          case 5:
            return 'TR-9'
            break
          case 6:
            return '钨碳'
            break
          case 7:
            return 'β钛'
            break
          case 8:
            return '纯钛'
            break
          case 9:
            return '混搭'
            break
          case 10:
            return '太阳钴'
            break
          case 11:
            return '聚酰胺'
            break
          case 12:
            return '包金'
            break
        }
      },
      accessoriesType(row, column, cellValue, index) {
        switch (cellValue) {
          case 1:
            return '框镜'
            break
          case 2:
            return '隐形'
            break
        }
      },
      lensMaterial(row, column, cellValue, index) {
        switch (cellValue) {
          case 1:
            return '树脂'
            break
          case 2:
            return '玻璃'
            break
          case 3:
            return 'PC'
            break
        }
      },
      usrType(row, column, cellValue, index) {
        switch (cellValue) {
          case 1:
            return '常带型'
            break
          case 2:
            return '抛弃型'
            break
          case 3:
            return '塑形镜'
            break
        }
      },
      disusedClassification(row, column, cellValue, index) {
        switch (cellValue) {
          case 1:
            return '日抛'
            break
          case 2:
            return '周抛'
            break
          case 3:
            return '月抛'
            break
          case 4:
            return '季抛'
            break
          case 5:
            return '半年抛'
            break
          case 6:
            return '年抛'
            break
          case 7:
            return 'RGP'
            break
          case 8:
            return '塑形镜'
            break
          case 9:
            return '双周抛'
            break
        }
      },
      unitType(row, column, cellValue, index) {
        switch (cellValue) {
          case '01':
            return '片';
            break;
          case '02':
            return '盒';
            break;
          case '03':
            return '副';
            break;
          case '04':
            return '瓶';
            break;
          case '05':
            return '张';
            break;
          case '06':
            return '把';
            break;
          case '07':
            return '个';
            break;
          case '08':
            return '只';
            break;
          case '09':
            return '套';
            break;
          case '10':
            return '天';
            break;
          case '11':
            return '包';
            break;
        }
      },
    }
  };
</script>
